package com.cn.wanxi.util;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class IOTool {
    private static File file;
//    private static final String SPLIT = "\n";

    /**
     * 默认方法应该是私有的，
     * 应该没有返回值
     * 方法名称：应该见名知意
     */
    public static void createFile(String fileName) {
//        String path = "E:\\wanxi\\step-3\\canyin03-0913\\web";
//        String direcory = "note";
//        String fileName = "company.txt";
        File fileDircory = new File(FileNames.PATH, FileNames.DERECTORY);
        fileDircory.mkdirs();
        file = new File(fileDircory, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeData(List<String> data) {
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < data.size(); i++) {
                outputStream.write(data.get(i).getBytes(StandardCharsets.UTF_8));
                outputStream.write(FileNames.SPLIT.getBytes(StandardCharsets.UTF_8));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String readData() {
        StringBuilder result = new StringBuilder();
        try {
            InputStream inputStream = new FileInputStream(file);
            Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            int read = 0;
            while ((read = reader.read()) != -1) {
                char c = (char) read;
                result.append(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return String.valueOf(result);
    }
}
